<?php

namespace app\erp\controller;

use app\erp\model\Buildtpldb;
use app\erp\model\Cartdb;
use app\erp\model\OrderApplyEinvoicedb;
use app\erp\model\Orderdb;
use app\erp\model\UBuildtpldb;
use app\erp\model\UOrderdb;

class Cart extends Base
{
    protected $db;
    public $post = array();

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
        $this->db = new Cartdb();
        $this->db->param = $this->post;
    }

    public function ajaxList()
    {
        $return = $this->db->ajaxList();
        $this->layuiReturn($return, count($return));
    }

    // 申请发票审核通过
    public function saveApplyInfo()
    {
        $data = $this->post['save_data'];
        foreach ($data as $key => $val) {
            $save_update['product_area'] = $val['product_area'];
            $save_update['shuiwu_id'] = $val['shuiwu_id'];
            $save_update['product_firm'] = $val['product_firm'];
            $save_update['invoice_model'] = $val['invoice_model']?:"　";
            $save_update['goods_keywords'] = $val['goods_keywords'];
            $save_update['goods_unit'] = $val['goods_unit'];
            $save_update['apply_state'] = $this->post['apply_state'];
            $save_update['nopass_info'] = $this->post['nopass_info'];
            $save_update['diy_beizhu'] = $val['diy_beizhu'] ?? '';
            $this->db->where('cart_id', $val['cart_id'])->update($save_update);
        }
        $save_apply['order_no'] = $this->post['order_no'];
        $save_apply['admin_id'] = MEMBER_ID;
        $save_apply['log'] = $this->post['apply_state'] == 3 ? '审核通过' : '驳回审核:' . $this->post['nopass_info'];
        $orderApplyEinvoicedb = new OrderApplyEinvoicedb();
        $orderApplyEinvoicedb->save($save_apply);
        $orderdb = new Orderdb();
        $get_order = $orderdb->where('order_no', $this->post['order_no'])->find();
        if ($get_order) {
            $get_order->save(['apply_state' => $this->post['apply_state'], 'apply_shehe_time' => time(), 'apply_audit_admin_id' => MEMBER_ID,'nopass_info'=>$this->post['nopass_info']]);
        }

        $UOrderdb = new UOrderdb();
        $UOrderdb->param = $this->post;
        $UOrderdb->param['apply_audit_admin_id'] = MEMBER_ID;
        $UOrderdb->applyEinvoiceState();

        if ($this->post['apply_state'] == 3) {
            // 如果审核通过 重新生成发票
            $get_order->save(['xsfp' => '生成中', 'cgfp' => '生成中']);
            $buildtpldb = new Buildtpldb();
            $save_data = [];
            $save_data['order_no'] = $get_order['order_no'];
            $save_data['buildtpl_type'] = 'xsfp';
            $save_data['state'] = 2;
            $buildtpldb->save($save_data);
            $buildtpldb = new Buildtpldb();
            $save_data = [];
            $save_data['order_no'] = $get_order['order_no'];
            $save_data['buildtpl_type'] = 'cgfp';
            $save_data['state'] = 2;
            $buildtpldb->save($save_data);
            $uorderdb =new UOrderdb();
            $get_u_order = $uorderdb->where('order_no',$this->post['order_no'])->find();
            if($get_u_order) {
                $ubuildtpldb = new UBuildtpldb();
                $save_data = [];
                $save_data['order_no'] = $get_order['order_no'];
                $save_data['buildtpl_type'] = 'xsfp';
                $save_data['state'] = 2;
                $ubuildtpldb->save($save_data);
                $ubuildtpldb = new UBuildtpldb();
                $save_data = [];
                $save_data['order_no'] = $get_order['order_no'];
                $save_data['buildtpl_type'] = 'cgfp';
                $save_data['state'] = 2;
                $ubuildtpldb->save($save_data);
            }
        }

        $this->layuiReturn('success');
    }
}